<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 列表区域 -->
      <el-table :data="rightsListPage" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="权限名称" prop="authName"></el-table-column>
        <el-table-column label="路径" prop="path"></el-table-column>
        <el-table-column label="权限等级" prop="level">
          <template slot-scope="scope">
            <el-tag v-show="scope.row.level === '0'">一级</el-tag>
            <el-tag type="success" v-show="scope.row.level === '1'"
              >二级</el-tag
            >
            <el-tag type="warning" v-show="scope.row.level === '2'"
              >三级</el-tag
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "Rights",
  data() {
    return {
      //权限列表
      rightsList: [],
      //控制页数的权限列表
      rightsListPage: [],
      //权限列表总数
      total: 0,
      //当前页
      currentPage: 1,
      //当前列表数
      currentPageSize: 10,
    };
  },
  methods: {
    //获取权限列表
    async getRightsList() {
      const { data: res } = await this.$http.get("rights/list");
      console.log(res);
      if (res.meta.status !== 200)
        return this.$message.error("获取权限列表失败");
      this.rightsList = res.data;
      this.total = res.data.length;
      this.rightsListPage = this.rightsList.slice(0, 10);
    },

    //每页多少条数据
    handleSizeChange(size) {
      this.currentPageSize = size;
      this.rightsList.forEach((element) => {
        if (this.rightsListPage.length == size) return;
        if (this.rightsList.length > size) {
          this.rightsListPage.splice(size - 1, this.rightsList.length);
        }
        this.rightsListPage.push(element);
      });
    },

    //当前页
    handleCurrentChange(page) {
      this.currentPage = page;
      let pageNum = this.currentPage * this.currentPageSize;
      if (pageNum < this.total) {
        this.rightsListPage = this.rightsList.slice(
           pageNum - this.currentPageSize,
          pageNum
        );
      }else{
        this.rightsListPage = this.rightsList.slice(pageNum - this.currentPageSize);
      }
    },
  },
  created() {
    this.getRightsList();
  },
};
</script>

<style lang="less" scoped>
</style>